﻿using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;

namespace Hont
{
    public class Node : MonoBehaviour
    {
        public Node[] siblingNodes = new Node[0];
        public bool alwaysDrawGizmos = false;

        public int F { get { return G + H; } }

        public int G { get; set; }

        public int H { get; set; }

        public int Mask { get; set; }
        public bool Walkable { get; set; }
        public int Cost { get; set; }
        public object UserData { get; set; }

        public Node Parent { get; set; }


        public void Init(int g, int h, bool walkable, int cost)
        {
            this.G = g;
            this.H = h;
            this.Walkable = walkable;
            this.Cost = cost;
        }

        public Node()
        {
            this.Walkable = true;
        }

        public bool CheckMask(int mask)
        {
            if (mask == -1) return true;

            return (mask & this.Mask) == this.Mask;
        }

        void OnDrawGizmos()
        {
            if (alwaysDrawGizmos)
                DrawGizmos();
        }

        void OnDrawGizmosSelected()
        {
            DrawSiblingNodes();

            if (!alwaysDrawGizmos)
                DrawGizmos();
        }

        void DrawGizmos()
        {
            Gizmos.DrawWireSphere(transform.position, 0.1f);
        }

        void DrawSiblingNodes()
        {
            var cacheColor = Gizmos.color;

            Gizmos.color = Color.yellow;
            for (int i = 0, iMax = siblingNodes.Length; i < iMax; i++)
            {
                var siblingNode = siblingNodes[i];

                if (siblingNode == null) continue;

                Gizmos.DrawWireSphere(siblingNode.transform.position, 0.12f);
            }

            Gizmos.color = cacheColor;
        }
    }
}
